<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;
use Weixin\Api\WxuserApi;
use Shop\Api\AddressApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ProductImageApi;
use Admin\Api\UserPictureApi;
use Shop\Api\CategoryApi;
class UserCenterController extends HomeController {

    /*
     * 用户中心首页
     * */
    public function index(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $result = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign($result['info']);
        $this->display();
    }

    /*
     * 用户信息
     * */
    public function userinfo(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $result = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign($result['info']);
        $this->display();
    }

    /*
     * 新增地址
     * */
    public function add_addre(){
        if(IS_POST){
            $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
            $result = apiCall(WxuserApi::GET_INFO , array($map));
            $_POST['uid'] = $result['info']['id'];
            $address = explode(' ',$_POST['address']);
            $_POST['province'] = $address['0'];
            $_POST['city'] = $address['1'];
            $_POST['area'] = $address['2'];
            $_POST['address'] = null;
            $res = apiCall(AddressApi::ADD);
            if($res['status']){
                if(!empty(I('product_id'))){
                    if(!empty($type=I('type'))){
                        if($type==1){
                            $this->success('添加成功',U('Group/buy_good',array('id'=> I('product_id'),'addressid' => $res['info'])));
                        }else{
                            $this->success('添加成功',U('User/buy_good',array('id'=> I('product_id'),'addressid' => $res['info'])));
                        }
                    }
                    $this->success('添加成功',U('User/buy_good',array('id'=> I('product_id'),'addressid' => $res['info'])));
                }else{
                    $this->redirect('UserCenter/addre_manage');
                }
            }else{
                $this->error('添加失败');
            }
        }else{
            if(!empty(I('product_id'))){
                if(!empty(I('type'))&&I('type')==1){
                    $this->assign('product_id',I('product_id'));
                    $this->assign('type',I('type'));
                }else{
                    $this->assign('product_id',I('product_id'));
                }
            }
            $this->display();
        }
    }
    /*
     * 地址管理
     * */
    public function addre_manage(){
        $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
        $user = apiCall(WxuserApi::GET_INFO , array($map));
        $this->assign('d_addr',$user['info']['default_address']);
        $map = array('uid'=>$user['info']['id']);
        $result = apiCall(AddressApi::QUERY_NO_PAGING , array($map));
        $this->assign("addr",$result['info']);
        $this->display();
    }
    /*
     * 地址删除
     * */
    public function addre_delete(){
        $map = array('id' => I('get.id'));
        $res = apiCall(AddressApi::DELETE , array($map));
        if($res['status']){
            echo(true);
        }else{
            echo(false);
        }
    }
    /*
     * 地址编辑
     * */
    public  function addre_edit(){
        if(IS_POST){
            $map = array('id' => $_POST['id']);
            $address = explode(' ',$_POST['address']);
            $_POST['province'] = $address['0'];
            $_POST['city'] = $address['1'];
            $_POST['area'] = $address['2'];
            $_POST['address'] = null;
            $res = apiCall(AddressApi::SET_INC , array($map, $_POST));
            if($res['status']){
                $this->redirect('UserCenter/addre_manage');
            }else{
                $this->error('修改失败');
            }
        }else{
            $map = array('id'=>I('get.id'));
            $addre = apiCall(AddressApi::GET_INFO , array($map));
            if($addre['status']){
                $this->assign($addre['info']);
            }
            $this->display();
        }

    }
    /*
     * 设置默认地址AJAX
     * */
    public function set_addre(){
        if(IS_GET){
            $map = array('openid' => $this -> openid, 'wxaccount_id' => $this -> wxaccount['id']);//wxaccount_id表示所属的微信公众账号
            $default = array('default_address' => I('get.id') );
            $res = apiCall(WxuserApi::SET_INC , array($map, $default));
            if($res['status']){
                echo(true);
            }else{
                echo(false);
            }
        }
    }
    /*
     * 我发布的历史
     * */
    public function  issue_his(){
        $map['uid'] = $this->userinfo['uid'];
        $map['status'] = array('in','-2,0,1');//表示要调出所有商品-2指的是被下架的商品
        $res = apiCall(ProductApi::ALLQUERY, array($map,'','createtime desc','','id,uid,name,status,img_id,type'));
        $productimglist = [];
        foreach($res['info']['list'] as $vo){
            $imgcondition['id'] = $vo['img_id'];
            $img = apiCall(UserPictureApi::GET_INFO, array($imgcondition));
            $skucondition['product_id'] = $vo['id'];
            $productsku = apiCall(ProductSkuApi::GET_INFO, array($skucondition));
            $productimglist[$vo['id']]['id'] = $vo['id'];
            $productimglist[$vo['id']]['name'] = $vo['name'];
            $productimglist[$vo['id']]['status'] = $vo['status'];
            $productimglist[$vo['id']]['imglist'][] = $img['info'];
            $productimglist[$vo['id']]['price'] = $productsku['info']['price'];
            $productimglist[$vo['id']]['ori_price'] = $productsku['info']['ori_price'];
        }
        $this->assign('product',$productimglist);
        $this->display();
    }
    /*
     * 编辑商品
     * */
    public function edit_product(){
        if(IS_GET){
            $map['id'] = I('id');//商品ID
            $map['uid'] = $this->userinfo['uid'];//用户ID
            $product = apiCall(ProductApi::GET_INFO, array($map));
            if(!$product['status']){
                $this->error('编辑打开失败哦');
            }else{
                //查找所属分类
                $categorymap['id'] = $product['info']['cate_id'];
                $category = apiCall(CategoryApi::GET_INFO,array($categorymap));
                $catenamearr[] = $category['info']['name'];
                $cateidarr[] = $category['info']['id'];
                while($category['info']['parent'] != 0){
                    $catemap['id'] = $category['info']['parent'];
                    $category = apiCall(CategoryApi::GET_INFO,array($catemap));
                    $catenamearr[] = $category['info']['name'];
                    $cateidarr[] = $category['info']['id'];
                }
                //组装出最后需要的分类信息
                $catenamestr = implode(' ',array_reverse($catenamearr));
                $cateidstr = implode(',',array_reverse($cateidarr));
                $this->assign('catenamestr',$catenamestr);
                $this->assign('cateidstr',$cateidstr);
                $conditionsku['product_id'] = I('id');
                $price = apiCall(ProductSkuApi::GET_INFO, array($conditionsku));
                if($price['status']){
                    //通过商品ID查询图片
                    $conditionimg['pid'] = I('id');
                    $imageids = apiCall(ProductImageApi::QUERY_NO_PAGING, array($conditionimg,'','img_id'));

                    if($imageids['status']){
                        foreach($imageids['info'] as $vo){
                            $arrid[] = $vo['img_id'];
                        }
                        $imgmap[] = 'in';
                        $imgmap[] = implode(',',$arrid);
                        $imgselect['id'] = $imgmap;
                        $imgres = apiCall(UserPictureApi::QUERY_NO_PAGING, array($imgselect));
                        $res = apiCall(CategoryApi::QUERY,array('','','','','id,name,parent'));
                        if($imgres['status'] && $res['status']){
                            //数组分类树的生成
                            $feilei = create_tree($res['info']['list']);
                            $json = json_encode($feilei);
                            $this->assign('feilei',$json);
                            $this->assign('productinfo',$product['info']);
                            $this->assign('price',$price['info']);
                            $this->assign('imglist',$imgres['info']);
                            $this->display();
                        }
                    }
                }else{
                    $this->error('编辑打开失败哦');
                }

            }
        }else{
            $this->error('请以正常方式访问');
        }

    }

    /*
     * 编辑时删除图片AJAX仅仅是将图片在productimage表中的数据删除
     * */
    public function edit_del_picture(){
        $map['img_id'] = I("imgIds",-1);

        $delres = apiCall(ProductImageApi::DELPRITURE, array($map));
        echo $delres['status'];
    }
    /*
     * 编辑后的商品从新上传
     * */
    public function edit_product_post(){
        if(IS_POST){
            //存储商品信息
            $product_id = I('product_id');
            $model = M();
            $model -> startTrans();
            $flag = false;
            $cate_list = explode(',',I('cate_id'));
            $all = count($cate_list);
            for($i = $all-1;$i > 0;$i--){
                if($cate_list[$i] != 0){
                    $all = $cate_list[$i];
                    break;
                }
            }
            $productmap['id'] = $product_id;
            $entity = array(
                'uid' => $this->userinfo['uid'],
                'name' => I('name'),//商品名称
                'cate_id' => $all,//商品所属栏目ID
                'updatetime' => date('U'),//更新时间
                'onshelf' => '0',//是否上架
                'status' => 1,//商品状态
                'synopsis' => I('synopsis'),//商品描述
                'dt_origin_country' => I('dt_origin_country'),//商品来源
                'postage' => I('postage')
            );
            $productapires = apiCall(ProductApi::SAVE, array($productmap,$entity));

            //存储商品的价格与原价
            if(!$productapires['status']){
                $flag = true;
            }
            $skumap['product_id'] = $product_id;
            $sku = array(
                'ori_price' => I('ori_price'),
                'price' => I('price'),
                'product_id' => $product_id,
                'quantity' => I('quantity')
            );
            $productsku = apiCall(ProductSkuApi::SAVE, array($skumap,$sku));
             if(!$productsku['status']){
                 $flag = true;
             }

            //存储商品的相应图片这里应该要添加一个判断商品是否有需要图片
            if(!empty(I('img_list'))){
                foreach(I('img_list') as $k => $vo){
                    $img['pid'] = $product_id;
                    $img['img_id'] = $vo;
                    if($k == 0){
                        $img['type'] = 6015;//主显示图
                    }
                    $img['create_time'] = date('U');
                    $imgres = apiCall(ProductImageApi::ADD, array($img));
                }
                if(!$imgres['status']){
                    $flag = true;
                }
            }
            if($flag){
                $model->rollback();
                $this->error('修改失败');
            }else{
                $model->commit();
                $this->success('修改成功',U('UserCenter/issue_his'));
            }
        }else{
            $this->error('请以正确的方式访问');
        }
    }

    /*
     * 商品删除
     * 这里假删除 status=-1
     * */

    public function product_delete(){
        $map['id'] = I('id');
        $res = apiCall(ProductApi::PRETEND_DELETE, array($map));
        if($res['status']){
            echo true;
        }else{
            echo false;
        }
    }
}